export const baseUrl = 'http://192.168.123.228:8282/'
// export const baseUrl = 'http://subei.e0s.cn/prod-api/'
const responseStatusCallback = response => {
	let msg = response.data?.msg ? response.data?.msg : '网络错误'
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 2000
	})
	return Promise.reject(response)
}
// https://ygj.api.e0s.cn:30443/ygj-api
//  https://lishi.api.e0s.cn/ygj-api
const service = (url, payload = {}, method = 'GET') => {
	const token = uni.getStorageSync('token') || ''
	let header = {}
	if (token) header['Authorization'] = 'Bearer ' + token
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			data: payload,
			header,
			success: res => {
				let msg = res.data?.msg ? res.data?.msg : '网络错误'
				// console.info('请求数据:' + JSON.stringify(res))
				// console.info(res)
				if (res.statusCode == 200) {
					if (res.data.code !== 200) {

						if (res.data.code === 401) {
							uni.reLaunch({
								url: '/pages/logoin/logoin'
							})
							return
						}
						uni.showToast({
							title: msg,
							icon: 'none',
							duration: 2000
						})
						reject(msg)
						// return responseStatusCallback(res)
					} else {

						resolve(res.data)
					}

				} else {
					uni.showToast({
						title: msg,
						icon: 'none',
						duration: 2000
					})
					reject(msg)
					// return responseStatusCallback(res)
				}


			},
			fail: res => {
				let msg = res.data?.msg ? res.data?.msg : '网络错误'
				uni.showToast({
					title: msg,
					icon: 'none',
					duration: 2000
				})
				reject(msg)
				// console.log(res);
				// return responseStatusCallback(res)
			}
		})
	})

}

export default service